<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>修改员工信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="adminForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
      <el-form-item label="员工部门" prop="title">
        <el-select v-model="adminForm.server_id" placeholder="请选择" style="width: 400px">
          <el-option
              v-for="item in serverIdList"
              :key="item.serveName"
              :label="item.serveName"
              :value="item.serveName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工姓名" prop="name">
        <el-input v-model="adminForm.name" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="员工密码" prop="password">
        <el-input v-model="adminForm.password" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phonenumber">
        <el-input v-model="adminForm.phonenumber" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="员工职位" prop="level">
        <el-select v-model="adminForm.level" placeholder="请选择" style="width: 400px">
          <el-option
              v-for="item in levelList"
              :key="item.levelname"
              :label="item.levelname"
              :value="item.levelname">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="服务图片" prop="url">-->
<!--        <el-input v-model="adminForm.url" style="width: 400px"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="adminForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ccc">
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" v-loading.fullscreen.lock="fullscreenLoading" @click="submitForm('adminForm')">修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      serverIdList:[],
      levelList:[],
      fullscreenLoading: false,
      adminForm: {
        serveId: '',
        title:'',
        serve_time:'',
        servicename: '',
        enable: '',
        price:'',
      },
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
        ],
        phonenumber: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          {min: 11, max: 11, message: '请输入正确的手机号码', trigger: 'blur'}
        ],
        level: [
          {required: true, message: '请输入员工级别', trigger: 'blur'},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
            ],
        enable: [
          {required: true, message: '', trigger: 'change'}
        ],
      }
    };
  },
  methods: {
    loadlevelname(){
      console.log("开始执行loadlevelname```")
      let url="http://localhost:9081/admins/level"
      console.log("url="+url)
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .get(url).then((response)=>{
        console.log("服务器返回的数据,放入levelList:")
        console.log(response.data)
        let responseBody = response.data;
        this.levelList=responseBody.data
      })
    },
    loadServerId(){
      console.log("开始执行loadServerId```")
      let url="http://localhost:9081/serve"
      console.log("url="+url)
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .get(url).then((response)=>{
        console.log("服务器返回的数据,放入serverIdList:")
        console.log(response.data)
        let responseBody = response.data;
        this.serverIdList=responseBody.data
      })
    },
    submitForm(adminForm) {
      this.fullscreenLoading = true;
      setTimeout(() => {
            this.fullscreenLoading = false;
      let url = "http://localhost:9081/admins/" + this.$route.query.id +"/update";
      console.log('update,id= ...'+this.$route.query.id);
      console.log(url);
      let formData = this.qs.stringify(this.adminForm);
      console.log(formData);
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .post(url,formData).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改【' + this.adminForm.name + '】成功!',
            type: 'success'
          });
        } else {
          // this.$message.error(responseBody.message);
          let title = '操作失败';
          this.$alert(responseBody.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
        this.$router.push('/sys-admin/temp/admin/list')
      })
      }, 1500);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadservicesList() {
      this.fullscreenLoading = true;
      setTimeout(() => {
        this.fullscreenLoading = false;
        let id = this.$route.query.id;
        console.log(id);
        console.log('loadadminsList ...');
        let url = "http://localhost:9081/admins/selectupdate/" + id;
        console.log(url);
        this.axios
            .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
            .post(url).then((respose) => {
          let responseBody = respose.data;
          this.adminForm = responseBody.data;
        })
      }, 800);
    },
  },
  mounted() {
    this.loadservicesList();
    this.loadServerId();
    this. loadlevelname();
  }
}
</script>

<style scoped>

</style>